import { ipcMain, screen, shell } from 'electron'
import { mainWin } from '../views/main'
import pluginManager from './plugin-mgr'

class API {
  init() {
    ipcMain.on('msg-trigger', async (event, arg) => {
      event.returnValue = await this[arg.type](arg.args, event)
    })
  }

  //api
  async show({ isHide }: { isHide?: boolean }) {
    if (isHide) {
      return mainWin.hide()
    }

    mainWin.show()
  }

  async windowMoving({ mouseX, mouseY }) {
    const { x, y } = screen.getCursorScreenPoint()
    mainWin.win.setPosition(x - mouseX, y - mouseY)
  }

  async setExpandHeight(height: number) {
    if (height > 600) {
      height = 600
    }
    mainWin.setExpandHeight(height)
  }

  async open(args) {
    shell.openPath(args)
  }

  async openPlugin(args) {
    mainWin.openPlugin(args, pluginManager.plugins)
  }

  async exitPlugin() {
    mainWin.exitPlugin()
  }

  async elements() {
    return {
      plugins: pluginManager.plugins
    }
  }
}

export default new API()
